package com.zmcc.clients;

import com.google.gson.Gson;
import com.zmcc.entity.MidAXTemplate;
import com.zmcc.entity.MidResult;
import com.zmcc.entity.common.CommonRequest;
import com.zmcc.entity.common.CommonResponse;
import com.zmcc.sender.DefaultSyncClient;
import com.zmcc.sender.ISyncClient;
import com.zmcc.service.IMidRequest;
import com.zmcc.service.impl.MidRequestCommercial;
import com.zmcc.utils.WrapperUtils;

public class MidAXClient {

    private final ISyncClient syncClient;

    private final IMidRequest requestWrapper;

    private final String appkey;

    private final String appsecret;

    private final String mode;

    public MidAXClient(Builder builder) {
        this.appkey = builder.appkey;
        this.appsecret = builder.appsecret;
        this.mode = builder.mode;
        this.syncClient = DefaultSyncClient.getInstance();
        this.requestWrapper = MidRequestCommercial.getInstance();
    }

    public static class Builder {
        private String appkey;
        private String appsecret;
        private String mode;

        public Builder() {

        }

        public MidAXClient.Builder appkey(String appkey) {
            this.appkey = appkey;
            return this;
        }

        public MidAXClient.Builder appsecret(String appsecret) {
            this.appsecret = appsecret;
            return this;
        }

        public MidAXClient.Builder mode(String mode) {
            this.mode = mode;
            return this;
        }

        public MidAXClient build() {
            return new MidAXClient(this);
        }
    }

    public MidResult midAxBindSend(MidAXTemplate MidAXTemplate) {
        CommonRequest request = this.requestWrapper.getMidAxBindRequest(MidAXTemplate);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), MidResult.class);
    }

    public void midAxUnBindSend(String bindId,int coolDown) {
        CommonRequest request = this.requestWrapper.getMidAxUnBindRequest(bindId,coolDown);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        this.syncClient.fetch(request);
    }


    public void midAxDelaySend(String bindId,String delta) {
        CommonRequest request = this.requestWrapper.getMidAxDelayRequest(bindId,delta);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        this.syncClient.fetch(request);
    }

    public void midAxBlacklistSend(String blackList,String opType) {
        CommonRequest request = this.requestWrapper.getMidAxBlacklistRequest(blackList,opType);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        this.syncClient.fetch(request);
    }
}
